<?php
	/** 发布求租求购房源类 
	@author ffpan
	@date 2010-05-8
	@version 1.0
	**/

	class Qgqzhouse{
		function __construct(){
			$this->fangDb = new wbDatabase();
			$this->order = array(
				1 => "order by postdate desc", //更新日期排名
				2 => "order by infoid asc", //编号由小到大
				3 => "order by infoid desc", //编号由大到小
			);
		}

		function __destruct(){}

		function __get($att){
			return $att;
		}

		function __set($att , $val){
			$this->$att = $val;
		}

		function safeVal($val){
			if (!get_magic_quotes_gpc()) {
				 $val = addslashes($val);
			}
			return $val;
		}

		function safePostVal(){
			if (!get_magic_quotes_gpc()) {
			   foreach($_POST as &$v){
				 $v = addslashes($v);
			   }
			}
			return $val;
		}
		
		
		
		//注册一个求购房源
		function addOneQgHouse(){
			$this->safePostVal();
			$title = $_POST["title"];
			$infoid = $_POST["infoid"];
			$requirement = $_POST["requirement"];
			$cname = $_POST["cname"];
			$mobile = $_POST["mobile"];
			$tips = $_POST["tips"];
			$ispublic = $_POST["ispublic"];
			$ruserid = $_POST["ruserid"];
			$ruserprop = $_POST["ruserprop"];
			$type = $_POST["type"];

			$sql = "insert into ".__table_qghouse__." 
					values(
					'',
					'".$title."',
					'".$infoid."',
					'".$requirement."',
					'".$cname."',
					'".$mobile."',
					'".$tips."',
					now(),
					'".$ruserid."',
					'".$ruserprop."',
					".$ispublic.",
					'".$type ."'
					)";
			$result = $this->fangDb->query($sql);
			if($result){
				if(__user_prop__=="0"&&$ispublic=="1"){
					$sql  = "update  ".__table_agentlist__." set scores = scores + ".__upload_score__." where id = ".__user_id__;
					$result = $this->fangDb->query($sql);
				}
				return "success";
			}else{
				return "fail";
			}
		}
		
		//注册一个求租房源
		function addOneQzHouse(){
			$this->safePostVal();
			$title = $_POST["title"];
			$infoid = $_POST["infoid"];
			$requirement = $_POST["requirement"];
			$isfull = $_POST["isfull"];
			$cname = $_POST["cname"];
			$mobile = $_POST["mobile"];
			$ispublic = $_POST["ispublic"];
			$tips = $_POST["tips"];
			$ruserid = $_POST["ruserid"];
			$ruserprop = $_POST["ruserprop"];

			$sql = "insert into ".__table_qzhouse__." 
					values(
					'',
					'".$title."',
					'".$infoid."',
					'".$requirement."',
					'".$isfull."',
					'".$cname."',
					'".$mobile."',
					'".$tips."',
					now(),
					'".$ruserid."',
					'".$ruserprop."',
					".$ispublic.",
					)";
			$result = $this->fangDb->query($sql);
			if($result){
				if(__user_prop__=="0"&&$ispublic=="1"){
					$sql  = "update  ".__table_agentlist__." set scores = scores + ".__upload_score__." where id = ".__user_id__;
					$result = $this->fangDb->query($sql);
				}
				return "success";
			}else{
				return "fail";
			}
		}
		
	
		//更新一个求购房源
		function updateOneQgHouse(){
			$this->safePostVal();
			$qgid = $_POST["qgid"];
			if(!isset($qgid)){return "fail";}
			$title = $_POST["title"];
			$infoid = $_POST["infoid"];
			$requirement = $_POST["requirement"];
			$cname = $_POST["cname"];
			$mobile = $_POST["mobile"];
			$tips = $_POST["tips"];

			
			$sql = "update ".__table_qghouse__." set
					title = '".$title."',
					infoid = '".$infoid."',
					requirement = '".$requirement."',
					cname = '".$cname."',
					mobile = '".$mobile."',
					tips = '".$tips."',
					postdate = now() 
					where id=".$qgid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		//更新一个求租房源
		function updateOneQzHouse(){
			$this->safePostVal();
			$qzid = $_POST["qzid"];
			if(!isset($qzid)){return "fail";}
			$title = $_POST["title"];
			$infoid = $_POST["infoid"];
			$requirement = $_POST["requirement"];
			$isfull = $_POST["isfull"];
			$cname = $_POST["cname"];
			$mobile = $_POST["mobile"];
			$tips = $_POST["tips"];

			
			$sql = "update ".__table_qzhouse__." set
					title = '".$title."',
					infoid = '".$infoid."',
					requirement = '".$requirement."',
					isfull = '".$isfull."',
					cname = '".$cname."',
					mobile = '".$mobile."',
					tips = '".$tips."',
					postdate = now() 
					where id=".$qzid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		//删除一个求购房源
		function deleteOneQgHouse(){
			$qgid = $this->safeVal($_GET["qgid"]);
			$sql = "delete from ".__table_qghouse__." where id=".$qgid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		//删除一个求租房源
		function deleteOneQzHouse(){
			$qzid = $this->safeVal($_GET["qzid"]);
			$sql = "delete from ".__table_qzhouse__." where id=".$qzid;
			$result = $this->fangDb->query($sql);
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		//上架|下架某一个求购|求租房源
		function updateListQgHouse($upordown){
			$ids = $_GET["ids"];
			if($upordown==1){
				$arr = explode("," , $ids);
				$num = count($arr) * __upload_score__;
				$sql  = "update  ".__table_agentlist__." set scores = scores + ".$num." where id = ".__user_id__;
				$result = $this->fangDb->query($sql);
			}
			$sql = "update ".__table_qghouse__." set ispublic = ".$upordown." where id in (".$ids.")";
			$result = $this->fangDb->query($sql);
			//echo $sql;
			if($result){
				return "success";
			}else{
				return "fail";
			}
		}
		
		function getQghouseInfo($id){
			$sql = "select * from ".__table_qghouse__." where id = ".$id." limit 1";
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_assoc();
			return $row;
		}
		
		function liveCondtion($condition){
			if($condition==""){return 1;}
			$arr = array();
			foreach($condition as $key=>$value){
				$value  = $this->safeVal($value);
				if($value==""){continue;}
				if($key=="title"){$arr[] = $key." like '%".$value."%' ";continue;}
				$arr[] = $key."='".$value."' ";
			}
			return implode(" and " , $arr);
		}
		
		
		function getIndexNewList($condition , $order , $p=-1 , $limit){
			$where = $this->liveCondtion($condition);
			$limit = " limit 0 , ".$limit;
			
			$sql = "select * from ".__table_qghouse__." where ".$where." ".$this->order[$order].$limit;
			$arr = array();
			
			if ($result = $this->fangDb->query($sql)){
			   while ($row = $result->fetch_assoc()) {
					$row["postdate"] = $this->getshowtime($row["postdate"]);
				    $arr[] = $row;
			   }
			   $result->close();
			}
			$this->listArr = $arr; 

			return $arr;
		}
		
		function getQgList($condition , $order , $p=-1){
			$where = $this->liveCondtion($condition);
			
			if($p!=-1){
				$pt = $this->getSearchPage($condition , __admin_qghouse_num__);
				if($p<1){$p=1;}
				if($p>$pt+1){$p = $pt+1;}

				$limit = " limit ".($p-1)*__admin_qghouse_num__." , ".__admin_qghouse_num__;
			}

			$sql = "select * from ".__table_qghouse__." where ".$where." ".$this->order[$order].$limit;
			$arr = array();
			
			if ($result = $this->fangDb->query($sql)){
			   while ($row = $result->fetch_assoc()) {
					$row["postdate"] = $this->getshowtime($row["postdate"]);
				    $arr[] = $row;
			   }
			   $result->close();
			}
			//echo $sql;
			$this->listArr = $arr; 

			return $arr;
		}
		
		function getSearchNum($condition){
			$where = $this->liveCondtion($condition);
			$sql = "select count(*) from ".__table_qghouse__." where ".$where;
			$result = $this->fangDb->query($sql);
			$row = $result->fetch_array();
			return $row[0];
		}
		function getSearchPage($condition , $pernum){
			$temp = $this->getSearchNum($condition);
			$page =  ceil($temp/$pernum);
			return $page;
		}
		
		function getshowtime($time){
			$t = explode(" " , $time);
			$t0 = explode("-" , $t[0]);
			$t1= explode(":" , $t[1]);

			return ($t0[1]."-".$t0[2]." ".$t1[0].":".$t1[1]);
		}

	}

	$myQgqzhouse = new Qgqzhouse();
?>